<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
			<span class="layui-breadcrumb">
            <a href="../index.html">首页</a>
            <a>
              <cite>日程</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div style="padding-top: 15px">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span>我的代办</span>
                    <!--<form class="layui-form layui-col-space5" style="float: right;">
                        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
                    <button class="layui-btn" onclick="xadmin.open('添加用户','./member-add.html',600,400)"><i class="layui-icon"></i>添加</button>
                    </form>-->

                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="todoTable" lay-filter="test"></table>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="start">开始</a>
                        <a class="layui-btn layui-btn-xs" lay-event="finish">完成</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['laydate', 'form', 'laypage', 'layer', 'table', 'element'], function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var laypage = layui.laypage;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var formSelects = layui.formSelects;
        var $=layui.jquery;

        var todoAdd,todoEdit;
        var todoPriority,todoStatus;
        var todoId;

        //实例化一个table
        table.render({
            elem: '#todoTable'
            ,id:'todoTable'
            , height: 420
            ,url: 'http://localhost:8080/todo/mytodo' //数据接口
            , title: '日程表'
            , page: true //开启分页
            , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,defaultToolbar:['filter', 'exports']
            , cols: [[ //表头
                {
                    type: 'radio',
                    fixed: 'left'
                }, {
                    field: 'id',
                    title: 'ID',
                    hide: true,
                }, {
                    field: 'todoName',
                    title: '名称',
                    width: '20%',
                    sort: true
                }, {
                    field: 'todoPriority',
                    title: '级别',
                    width: '10%',
                    sort: true
                },{
                    field: 'todoStartDate',
                    title: '开始时间',
                    width: '10%',
                    sort: true
                }, {
                    field: 'todoCreateDate',
                    title: '创建时间',
                    width: '10%',
                    sort: true
                }, {
                    field: 'todoEndDate',
                    title: '结束结束',
                    width: '10%',
                    sort: true
                }, {
                    field: 'todoCreater',
                    title: '创建者',
                    width: '10%',
                    sort: true
                }, {
                    field: 'todoStatus',
                    title: '状态',
                    width: '10%',
                    sort: true
                }, {
                    fixed: 'right',
                    width: '20%',
                    minWidth: 100,
                    align: 'center',
                    toolbar: '#barDemo'
                }
            ]]
            ,done:function (res,curr,count) {
                if(res.data==null){
                    layer.msg('用户未登录或该用户没有代办事项', {
                        icon: 5,
                        time:2000
                    });
                }
            }
        });

        form.on('select(todoPriority)',function (data) {
            todoPriority=data.value;
        });
        // form.on('select(todoStatus)',function (data) {
        //     todoStatus=data.value;
        // });
        laydate.render({
            elem:'#todoStartDate'
            ,type: 'datetime'
        });
        laydate.render({
            elem:'#todoEndDate'
            ,type: 'datetime'
        });
        form.on('select(todoPriority_edit)',function (data) {
            todoPriority=data.value;
        });
        // form.on('select(todoStatus)',function (data) {
        //     todoStatus=data.value;
        // });
        laydate.render({
            elem:'#todoStartDate_edit'
            ,type: 'datetime'
        });
        laydate.render({
            elem:'#todoEndDate_edit'
            ,type: 'datetime'
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id),
                data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    // xadmin.open('添加代办事项','./todo-add.html');
                    todoAdd=layer.open({
                        type:1,
                        title:'添加代办',
                        area:['600px','500px'],
                        content:$('#todo_add')
                    });
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        $("#todoName_edit").val(data[0].todoName);
                        $("#todoDesc_edit").val(data[0].todoDesc);
                        todoId=data[0].id;

                        todoEdit=layer.open({
                            type:1,
                            title:'修改代办',
                            area:['600px','500px'],
                            content:$('#todo_edit')
                        });
                    }
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'start') {
                // xadmin.open('查看选中的事项','./todo-content.html');
                if(data.todoStatus=="进行中"||data.todoStatus=="已完成"){
                    layer.msg('已经开始或完成的代办不能开始',{
                        icon: 5,
                        time: 2000
                    });
                }else{
                    var myDate=new Date();
                    //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
                    var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                        myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
                    data.todoStartDate=nowDate;
                    // layer.msg(nowDate);
                    $.ajax({
                        url: 'http://localhost:8080/todo/start',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('设置成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    table.reload('todoTable',{
                                        url:'http://localhost:8080/todo/mytodo'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                }
            }
            else if (layEvent === 'del') {
                layer.confirm('删除后将不可恢复',{icon:3,title:'提示'}, function(index){
                    obj.del();
                    layer.close(index);
                    $.ajax({
                        url: 'http://localhost:8080/todo/delete',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('删除成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    //添加用户成功后关闭弹窗并刷新
                                    layer.close(index);
                                    table.reload('todoTable',{
                                        url:'http://localhost:8080/todo/mytodo'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                });
            }
            else if (layEvent === 'finish') {
                // xadmin.open('查看选中的事项','./todo-content.html');
                if(data.todoStatus!="进行中"){
                    layer.msg('只能完成进行中的代办',{
                        icon: 5,
                        time: 2000
                    });
                }else{
                    var myDate=new Date();
                    //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
                    var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                        myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
                    data.todoEndDate=nowDate;
                    // layer.msg(nowDate);
                    $.ajax({
                        url: 'http://localhost:8080/todo/finish',
                        data: data,
                        contentType: "application/x-www-form-urlencoded",
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        success: function(data) {
                            if(data.status == "success") {
                                layer.msg('设置成功', {
                                    icon: 1,
                                    time: 2000
                                }, function() {
                                    table.reload('todoTable',{
                                        url:'http://localhost:8080/todo/mytodo'
                                    })
                                });
                            } else {
                                layer.msg(data.data.errMsg, {
                                    icon: 5,
                                    time:1000
                                });
                            }
                        }
                    });
                    return false;
                }
            }
        });

        form.on('submit(create_todo)',function (obj) {
            //获取表单数据
            var field = obj.field;
            field.todoPriority=todoPriority;
            // field.todoStatus=todoStatus;
            var myDate=new Date();
            //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
            var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
            field.todoCreateDate=nowDate;
            $.ajax({
                url: 'http://localhost:8080/todo/create',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('创建代办成功', {
                            icon: 1,
                            time: 2000
                        }, function() {
                            layer.close(todoAdd);
                            table.reload('todoTable',{
                                url:'http://localhost:8080/todo/mytodo'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:2000
                        });
                    }
                }
            });
            return false;
        });

        form.on('submit(edit_todo)',function (obj) {
            //获取表单数据
            var field = obj.field;
            field.todoPriority_edit=todoPriority;
            // field.todoStatus=todoStatus;
            var myDate=new Date();
            //获取当前时间并格式化yyyy-MM-dd HH:mm:ss
            var nowDate=myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+
                myDate.getDate()+" "+myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
            field.todoCreateDate_edit=nowDate;
            field.id=todoId;
            $.ajax({
                url: 'http://localhost:8080/todo/update',
                data: field,
                contentType: "application/x-www-form-urlencoded",
                type: 'POST',
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {
                    if(data.status == "success") {
                        layer.msg('修改代办成功', {
                            icon: 1,
                            time: 2000
                        }, function() {
                            layer.close(todoEdit);
                            table.reload('todoTable',{
                                url:'http://localhost:8080/todo/mytodo'
                            })
                        });
                    } else {
                        layer.msg(data.data.errMsg, {
                            icon: 5,
                            time:2000
                        });
                    }
                }
            });
            return false;
        });

    });
</script>
</body>
<!--添加代办-->
<div style="display: none;" id="todo_add">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>标题
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="todoName" name="todoName" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>优先级
                    </label>
                    <div class="layui-input-inline">
                        <select name="todoPriority" id="todoPriority" lay-filter="todoPriority" required="" lay-verify="required">
                            <option value="">请选择优先级</option>
                            <option value="最低">最低</option>
                            <option value="一般">一般</option>
                            <option value="较高">较高</option>
                            <option value="最高">最高</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>开始
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="todoStartDate" id="todoStartDate" required="" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>结束
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="todoEndDate" id="todoEndDate" required="" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" style="width: 600px">
                    <label class="layui-form-label"><span class="x-red">*</span>描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" class="layui-textarea" name="todoDesc" id="todoDesc"></textarea>
                    </div>
                </div>
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">-->
                        <!--<span class="x-red">*</span>状态-->
                    <!--</label>-->
                    <!--<div class="layui-input-inline">-->
                        <!--<select name="todoStatus" id="todoStatus" lay-filter="todoStatus">-->
                            <!--<option value=""></option>-->
                            <!--<option value="未开始">未开始</option>-->
                        <!--</select>-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                    <!--<div class="layui-inline">-->
                        <!--<label class="layui-form-label">起止时间</label>-->
                        <!--<div class="layui-input-inline">-->
                            <!--<input type="text" name="todoStart" id="todoStart" lay-verify="time" placeholder="HH:mm:ss"-->
                                   <!--autocomplete="off" class="layui-input">-->
                        <!--</div>-->
                        <!--<div class="layui-form-mid">-</div>-->
                        <!--<div class="layui-input-inline">-->
                            <!--<input type="text" name="todoEnd" id="todoEnd" lay-verify="time" placeholder="HH:mm:ss"-->
                                   <!--autocomplete="off" class="layui-input">-->
                        <!--</div>-->
                    <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-radius layui-btn-fluid" lay-filter="create_todo" lay-submit="">
                        提交
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<!--编辑代办-->
<div style="display: none;" id="todo_edit">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span class="x-red">*</span>标题
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="todoName_edit" name="todoName_edit" required="" lay-verify="required"
                               autocomplete="off" class="layui-input" disabled>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>优先级
                    </label>
                    <div class="layui-input-inline">
                        <select name="todoPriority_edit" id="todoPriority_edit" required="" lay-verify="required" lay-filter="todoPriority_edit">
                            <option value="">请选择优先级</option>
                            <option value="最低">最低</option>
                            <option value="一般">一般</option>
                            <option value="较高">较高</option>
                            <option value="最高">最高</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>开始
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="todoStartDate_edit" id="todoStartDate_edit" required="" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label  class="layui-form-label">
                        <span class="x-red">*</span>结束
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" name="todoEndDate_edit" id="todoEndDate_edit" required="" lay-verify="required" placeholder="yyyy-MM-dd HH:mm:ss"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" style="width: 600px">
                    <label class="layui-form-label"><span class="x-red">*</span>描述</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" class="layui-textarea" name="todoDesc_edit" id="todoDesc_edit"></textarea>
                    </div>
                </div>
                <!--<div class="layui-form-item">-->
                    <!--<label class="layui-form-label">-->
                        <!--<span class="x-red">*</span>状态-->
                    <!--</label>-->
                    <!--<div class="layui-input-inline">-->
                        <!--<select name="todoStatus_edit" id="todoStatus_edit">-->
                            <!--<option value=""></option>-->
                            <!--<option value="未开始">未开始</option>-->
                        <!--</select>-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="layui-form-item">-->
                <!--<div class="layui-inline">-->
                <!--<label class="layui-form-label">起止时间</label>-->
                <!--<div class="layui-input-inline">-->
                <!--<input type="text" name="todoStart" id="todoStart" lay-verify="time" placeholder="HH:mm:ss"-->
                <!--autocomplete="off" class="layui-input">-->
                <!--</div>-->
                <!--<div class="layui-form-mid">-</div>-->
                <!--<div class="layui-input-inline">-->
                <!--<input type="text" name="todoEnd" id="todoEnd" lay-verify="time" placeholder="HH:mm:ss"-->
                <!--autocomplete="off" class="layui-input">-->
                <!--</div>-->
                <!--</div>-->
                <!--</div>-->
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-radius layui-btn-fluid" lay-filter="edit_todo" lay-submit="">
                        修改
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
</html>